home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7787 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  73 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.netins.net!isac!gg
  3. From: gg@isac.hces.com (Greg Goodrich)
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Message-ID: <1996Feb28.195423.10465@isac.hces.com>
  6. Organization: Health Care Expert Systems
  7. X-Newsreader: TIN [version 1.2 PL2]
  8. References: <4gqpa1$3h9@alcor.usc.edu> <1996Feb26.211807.28858@isac.hces.com> <31331a38.54160408@nntp.ix.netcom.com>
  9. Date: Wed, 28 Feb 1996 19:54:23 GMT
  10.  
  11. Mike Rubenstein (miker3@ix.netcom.com) wrote:
  12. : gg@isac.hces.com (Greg Goodrich) wrote:
  13.  
  14. : > Abu Wawda (wawda@alcor.usc.edu) wrote:
  15. : > : I'm having trouble understanding what the address of a static array
  16. : > : is. For example, if I declare a variable called myarray as:
  17. : > :     char myarray[10];
  18. : > : then what could &myarray possibly mean? myarray is not a pointer, so
  19. : > : &myarray could not possibly be the address of the variable myarray
  20. : > : (like it would be if I did char* myarray and then asked for &myarray).
  21. : > 
  22. : > : Functions such as scanf() allow the following:
  23. : > 
  24. : > :     char myarray[10];
  25. : > 
  26. : > :     scanf("%s",&myarray);
  27. : > 
  28. : > : but I don't understand what scanf() could possibly be taking in the
  29. : > : second parameter. It can't be: char** since myarray is not a
  30. : > : pointer. I CAN understand how the following would work:
  31. : > 
  32. : > This is because C treats the occurrence of array names as the address of
  33. : > the array.  Therefore the following are equivalent:
  34. : > 
  35. : >     scanf("%s", myarray);
  36. : >     scanf("%s", &myarray);
  37. : > 
  38. : > The thing is, the address is implied when you use an array name.  This
  39. : > is not so for pointers.  I am not sure why C was incorporated in this
  40. : > way.  In my humble opinion, it would make it easier and clearer to force
  41. : > the programmer to put the & in front of array names, the same as you
  42. : > have to do for any other storage class.
  43.  
  44. : No.  The are not equivalent.  The first is legal and the second is
  45. : not.
  46.  
  47. : The %s format item in scanf expects a pointer to char.  myarray is
  48. : converted to a pointer to char so it is legal.  &myarray is a pointer
  49. : to array of 10 char and is not converted.  This results in undefined
  50. : behavior.
  51.  
  52. : In many implementations pointer to char and pointer to array of 10
  53. : char have the same representation and this will work properly, but
  54. : this is not required by the standard.
  55.  
  56. I would like to see an example of how this could be implemented to fail.
  57. They both point to the exact same memory address, the only difference
  58. being the datatype of the value, which can be type casted if necessary.
  59. If you have an array, the address of the array is also the address of
  60. the first member of the array, because the array itself is not a
  61. pointer, but a reference.  The same goes for a structure.  The address
  62. of a struct is the same as the address of the first member of the
  63. struct, but of different data type.  One is pointer to struct and the
  64. other is pointer to whatever the first member is declared as, but they
  65. are both pointers, and therefore are the same size.
  66.  
  67. Greg.
  68. -- 
  69. _______________________________________
  70.     Greg Goodrich - gg@hces.com
  71.     Software Engineer
  72.     PACE Health Management Systems
  73.